package info.yaohua.servlet;

import info.yaohua.dao.Ac01Dao;
import info.yaohua.dto.Ac01Dto;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String registerMessage;
		HttpSession session = request.getSession();
		
		String aac001 = request.getParameter("aac001");
		String aac002 = request.getParameter("aac002");
		String aac003 = request.getParameter("aac003");
		String aac004 = request.getParameter("aac004");
		String aac005 = request.getParameter("aac005");
		String aac007 = request.getParameter("aac007");
		String aac009 = request.getParameter("aac009");
		
		if (aac001.equals("")||aac002.equals("")||aac003.equals("")||aac004.equals("")||aac005.equals(""))
		{
			registerMessage = "所有字段都不能为空！";
			request.setAttribute("registerMessage", registerMessage);
			//��ת��ע��ҳ��
		    RequestDispatcher view = request.getRequestDispatcher("/register.jsp");
		    view.forward(request, response);
		    
		}
		else
		{
			//�ж��û����Ƿ��ظ�
			Ac01Dao ac01Dao = new Ac01Dao();
			Ac01Dto ac01Dto = new Ac01Dto();
			ac01Dto.setAac001(aac001);
			
			try {
				ac01Dto = ac01Dao.doFindAc01ByAac001(ac01Dto);
				if (ac01Dto!=null)
				{
					registerMessage = "用户名重复！";
					request.setAttribute("registerMessage", registerMessage);
					//返回注册页面
				    RequestDispatcher view = request.getRequestDispatcher("/register.jsp");
				    view.forward(request, response);
				}
				else
				{
					ac01Dto = new Ac01Dto();
					ac01Dto.setAac001(aac001);
					ac01Dto.setAac002(aac002);
					ac01Dto.setAac003(aac003);
					ac01Dto.setAac004(aac004);
					ac01Dto.setAac005(aac005);
					ac01Dto.setAac007(aac007);
					ac01Dto.setAac009(aac009);
					ac01Dto.setAac006("000");
					ac01Dto.setAac008(new Date(0));
					int success = ac01Dao.doStore(ac01Dto);
					if (success !=0)
					{
						registerMessage = "注册成功！";
						request.setAttribute("registerMessage", registerMessage);
						//��ת��ע��ҳ��
					    RequestDispatcher view = request.getRequestDispatcher("/login.jsp");
					    view.forward(request, response);
					}
				}
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	
	}

}
